﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace PBLOG
{
    public class LogsCollectionBase : ICollection<Log>
    {
        private LogType _logType;

        private List<Log> _logs;

        public LogsCollectionBase(LogType logType)
        {
            if (logType == null || logType == LogType.None)
                throw new ArgumentNullException();

            this._logType = logType;

            this._logs = XmlHelper.Logs.GetLogs().Where(l => l.LogType == this._logType).ToList();
        }

        public Log this[int index]
        {
            get
            {
                return this._logs[index];
            }
            set
            {
                this._logs[index] = value;

                this.Add(value);
            }
        }

        public void Add(Log item)
        {
            item.LogType = this._logType;

            LogsCollectionHelper.Add(item);
        }
       
        public void Add(string message)
        {
            LogsCollectionHelper.Add(message, this._logType);
        }

        public void Add(string message, User user)
        {
            LogsCollectionHelper.Add(message, user, this._logType);
        }

        public void Add(string message, string username)
        {
            LogsCollectionHelper.Add(message, username, this._logType);
        }

        public void Clear()
        {
            LogsCollectionHelper.Clear(this._logType);
        }

        public bool Contains(Log item)
        {
            item.LogType = this._logType;

            return LogsCollectionHelper.Contains(item);
        }

        public bool Contains(string message)
        {
            return LogsCollectionHelper.Contains(message);
        }

        public void CopyTo(Log[] array, int arrayIndex)
        {
            LogsCollectionHelper.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this._logs.Count; }
        }

        public bool IsReadOnly
        {
            get { return LogsCollectionHelper.IsReadOnly; }

            //get { return false; }
        }

        public bool Remove(Log item)
        {
            item.LogType = this._logType;

            return LogsCollectionHelper.Remove(item);
        }

        public IEnumerator<Log> GetEnumerator()
        {
            return LogsCollectionHelper.GetEnumerator();

            //return this._logs.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._logs.GetEnumerator();
        }
    }
}